import { contextBridge, ipcRenderer } from 'electron'

// 安全地暴露 Electron API 到渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 系统相关 API
  getPlatform: () => ipcRenderer.invoke('get-platform'),
  getAppVersion: () => ipcRenderer.invoke('get-app-version'),
  openExternal: (url: string) => ipcRenderer.send('open-external', url),

  // 文件系统 API
  readFile: (filePath: string) => ipcRenderer.invoke('read-file', filePath),

  // 窗口控制 API
  minimizeWindow: () => ipcRenderer.send('minimize-window'),
  maximizeWindow: () => ipcRenderer.send('maximize-window'),
  closeWindow: () => ipcRenderer.send('close-window'),

  // 自定义事件监听
  on: (channel: string, callback: Function) => {
    const newCallback = (_: any, data: any) => callback(data)
    ipcRenderer.on(channel, newCallback)
    return () => ipcRenderer.removeListener(channel, newCallback)
  }
})